package com.jeesite.modules.isys.keys.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jeesite.modules.isys.keys.entity.HistoryAccount;
import com.jeesite.modules.isys.keys.entity.Lab;
import com.jeesite.modules.isys.keys.mapper.HistoryAccountMapper;
import com.jeesite.modules.isys.keys.mapper.LabMapper;
import com.jeesite.modules.isys.keys.service.ILabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XYMTOP
 * @since 2023-11-12
 */
@Controller
@RequestMapping("/mybatisplus/historyAccount")
public class HistoryAccountController {

    @Autowired
    private HistoryAccountMapper historyAccountMapper;
    @Autowired
    private ILabService ILabService;

    @RequestMapping("/borrow")
    public Map<String, Object> borrowRecord(@RequestBody HistoryAccount historyAccount) {
        Map<String, Object> map = new HashMap<>();
        List<String> labList = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14");
        boolean labExist = labList.stream().anyMatch(name -> name.equals(historyAccount.getLabId()));
        if (!labExist) {

            map.put("code", 400);
            map.put("msg", "Lab is not exist.");

        } else if (ILabService.getLabStatus(historyAccount.getLabId()) == 0) {
            historyAccountMapper.insert(historyAccount);
            ILabService.updateLabStatus(historyAccount.getLabId());
            map.put("code", 200);
            map.put("msg", "ok");

        } else {
            map.put("code", 401);
            map.put("msg", "Lab key has been borrowed.");
        }
        return map;

    }
}
